package cn.tedu.malls.product.webapi.service;

import cn.tedu.mall.sproduct.service.ICategoryService;
import cn.tedu.malls.commons.exception.ServiceException;
import cn.tedu.malls.commons.response.ServiceCode;
import cn.tedu.malls.pojo.dto.CategoryAddNewDTO;
import cn.tedu.malls.pojo.entity.Category;
import cn.tedu.malls.pojo.vo.CategoryListItemVO;
import cn.tedu.malls.pojo.vo.CategoryStandardVO;
import cn.tedu.malls.product.webapi.cache.repo.ICategoryCacheRepository;
import cn.tedu.malls.product.webapi.mapper.CategoryMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CategoryServiceImpl implements ICategoryService {
    @Autowired
    CategoryMapper categoryMapper;
    @Autowired
    ICategoryCacheRepository categoryCacheRepository;
    public static final Integer REBUILD_INTERVAL_TIME_IN_MINUTE = 1;


    @Override
    public CategoryStandardVO getById(Long id) {
        CategoryStandardVO categoryStandardVO = categoryMapper.getById(id);
        if(categoryStandardVO == null){
            throw new ServiceException(ServiceCode.NOT_FOUND,"类别不存在!");
        }
        return categoryStandardVO;
    }

    @Override
    public List<CategoryListItemVO> list() {
        List<CategoryListItemVO> list = categoryCacheRepository.list();
        return list;
    }

    private void loadCache(){
        List<CategoryListItemVO> categories = categoryMapper.list();
        List<CategoryStandardVO> listDetails = categoryMapper.listDetails();
        categoryCacheRepository.clear();
        categoryCacheRepository.appendList(categories);
        for (CategoryStandardVO category:listDetails){
            categoryCacheRepository.setCategory(category);
        }
        categoryCacheRepository.setRecentRebuildTime(System.currentTimeMillis());
    }

    @Override
    public void initCache() {
        loadCache();
    }

    @Override
    public void rebuildCache() {
        Long recentRebuildTime = categoryCacheRepository.getRecentRebuildTime();
        if(recentRebuildTime == null){
            recentRebuildTime = 0L;
        }
        Long currentRebuildTime = System.currentTimeMillis();
        if(currentRebuildTime-recentRebuildTime > REBUILD_INTERVAL_TIME_IN_MINUTE*60*1000){
            loadCache();
        }else {
            throw new ServiceException(ServiceCode.INTERNAL_SERVER_ERROR,"重建缓存失败，重建缓存必须间隔至少" + REBUILD_INTERVAL_TIME_IN_MINUTE + "分钟！");
        }
    }


    @Override
    public Long addNew(CategoryAddNewDTO categoryAddNewDTO) {
        CategoryStandardVO categoryStandardVO = categoryMapper.getByName(categoryAddNewDTO.getName());
        if(categoryStandardVO != null){
            throw new ServiceException(ServiceCode.NOT_ACCEPTABLE,"该用户名已存在!");
        }
        Long parentId = categoryAddNewDTO.getParentId();
        Integer depth = 1;
        CategoryStandardVO parentCategoryStandardVO = null;
        if(parentId != 0){
            parentCategoryStandardVO = categoryMapper.getById(parentId);
            if(parentCategoryStandardVO == null){
                throw new ServiceException(ServiceCode.NOT_ACCEPTABLE,"父级类别不存在!");
            }
             depth = parentCategoryStandardVO.getDepth() + 1;
        }
        Category category = new Category();
        BeanUtils.copyProperties(categoryAddNewDTO,category);
        category.setDepth(depth);
        category.setParent(0);
        category.setGmtCreate(LocalDateTime.now());
        category.setGmtModified(LocalDateTime.now());
        int rows = categoryMapper.addNew(category);
        if(rows != 1){
            throw new ServiceException(ServiceCode.NOT_ACCEPTABLE,"新增失败!");
        }
        if(parentCategoryStandardVO.getParent() != 1){
            categoryMapper.updateById(parentCategoryStandardVO.getId(),1);
        }
        return category.getId();
    }


}
